import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import Structure.Word;

public class Test {

	public String pPageB = "\\s*<page>\\s*";
	public String pPageE = "\\s*.*?</page>\\s*";
	/**
	 * Start regex for the not xml text 1 rest
	 */
	private Pattern pTextB = Pattern.compile("\\s*<text xml:space=\"preserve\">(.*)");
	/**
	 * end regex for the not xml text
	 */
	private String pTextE = ".*</text>";
	
	private String pSubCategory = "\\s*\\{\\{Referenzen\\}\\}\\s*";

	private Pattern pUebersetzung=Pattern.compile(
	  "\\s*==== .*?bersetzungen ====\\s*");
	
	public int countPages(String file) {

		int i = 0;
		int iTextB=0;
		int iTextE=0;
		int iSubcategory = 0;
		int iUebersetzung=0;
		
		try {
			BufferedReader bf = new BufferedReader(new FileReader(
					new File(file)));

			String line;
			while ((line = bf.readLine()) != null) {
				if (line.matches(pPageB)) {
					i++;
				}
				
				Matcher m=pTextB.matcher(line);
				if(m.find()){
					iTextB++;
				}
				if(line.matches(pTextE)){
					iTextE++;
				}
				if(line.matches(pSubCategory))
					iSubcategory++;
				if (pUebersetzung.matcher(line).matches())
					iUebersetzung++;
				
//				if (line.matches(pPageE)) {
//					i--;
//				}
//				if(i!=0&&i!=1)
//				System.out.println("big problem in little china: "+i);
			}

		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("iTextB: "+iTextB);
		System.out.println("iTextE: "+iTextE);
		System.out.println("iSubca: "+iSubcategory);
		System.out.println("iUeber: "+iUebersetzung);
		
		return i;
	}

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		String file = "dewiktionary-20111124-pages-articles.xml";
		Test t = new Test();
		System.out.println(t.countPages(file));		
	}

}
